package cn.org.donald.framework.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * @author ： Donald
 * @since ： 2020/10/24 22:49
 */
@Configuration
public class MybatisPlusConfig {
    /**
     * 注册乐观锁组件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    /**
     * 注册分页组件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }


    /**
     * 注册逻辑删除组件
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector(){
        return new LogicSqlInjector();
    }

    /**
     * 注册性能监视插件
     * @return
     */
    @Bean
    @Profile({"dev","test"})//dev、test环境激活
    public PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        /*sql允许执行最大时间(ms),超过就不执行,并抛出异常*/
        //performanceInterceptor.setMaxTime(2000);
        /*是否开启sql美化格式*/
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }
}
